﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DTO;
using BUS;

namespace CDT_CSDLNC
{
    public partial class ucXacNhanMacCa : UserControl
    {
        public ucXacNhanMacCa()
        {
            InitializeComponent();
        }

        public string matinban;
        public string manguoimua;
        public int giasanpham;
        public int soluongmua;
        public int giamacca;

        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            frmMain frmMain = (frmMain)(this.ParentForm);
            frmDongYMacCa frm = new frmDongYMacCa();
            frm.tensp = frmMain.ucctsp.tenSanPham;
            frm.giamacca = txtGiaDeNghi.Text;
            frm.soluongmua = txtSoLuong.Text;
            frm.hantraloi = lbNgayHan.Text;
            frm.tinnhan = txtTinNhan.Text;

            //thêm vào tin mặc cả
            TinMacCaDTO tmc = new TinMacCaDTO();
            tmc.NgayGui = DateTime.Now;
            tmc.MaTaiKhoanGui = manguoimua;
            tmc.MaTinBan = matinban;
            string manguoiban = TinBanBUS.GetMaNguoiBan(matinban);
            tmc.MaTaiKhoanNhan = manguoiban;
            tmc.SoLuongMua = int.Parse(txtSoLuong.Text);
            tmc.GiaNiemYet = giasanpham;
            tmc.GiaDeNghiTra = int.Parse(txtGiaDeNghi.Text);
            tmc.TinNhan = txtTinNhan.Text;
            tmc.HanTraLoi = DateTime.Parse(lbNgayHan.Text);
            tmc.ChoPhepMacCa = true;
            frm.maNguoiBan = manguoiban;
            try
            {
                soluongmua = tmc.SoLuongMua;
                giamacca = tmc.GiaDeNghiTra;
                TinMacCaBUS.ThemTinMacCa(tmc);
                frm.ShowDialog();
                MessageBox.Show("bên bán đã đồng ý bán sản phẩm này cho bạn");

                frmMain main = (frmMain)this.ParentForm;
                main.kb2_buoc = 5;
                main.KichBan2();
            }
            catch (Exception ex)
            {
                MessageBox.Show("lỗi thêm tin mặc cả " + ex.Message.ToString());
            }
        }

        private void ucXacNhanMacCa_Load(object sender, EventArgs e)
        {
            frmMain frm = (frmMain)(this.ParentForm);
            lbGiaNiemYet.Text = frm.ucctsp.donGia.ToString();
            txtSoLuong.Text = frm.ucctsp.soLuongMua.ToString();
            txtGiaDeNghi.Text = frm.ucctsp.giaDeNghi.ToString();
            giasanpham = frm.ucctsp.giaSanPham;

            cbbHanTraLoi.SelectedIndex = 0;
            lbNgayHan.Text = DateTime.Now.AddDays(1).ToString("MM/dd/yyyy");
        }

        private void cbbHanTraLoi_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = cbbHanTraLoi.SelectedIndex;
            lbNgayHan.Text = DateTime.Now.AddDays(i+1).ToString("MM/dd/yyyy");
        }
    }
}
